/*
 * Copyright (C) 2013 SeekDeepWithin.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.seekdeepwithin.Data;

import java.util.*;
import java.util.List;

/**
 * User: Jonathan Montiverdi
 * Date: Sep 26, 2013
 *
 * Represents a passage from seek deep within.
 */
public class Passage extends BaseDbObject
{
   private int m_Order;
   private int m_Number;
   private List<Link> m_Links = null;
   private List<Style> m_Styles = null;
   private BaseDbObject m_Header = null;
   private List<Link> m_Definitions = null;
   private Map<Integer, BaseDbObject> m_Footers = null;

   /**
    * Initializes a new passage.
    * @param id ID of passage.
    * @param text Text of Passage.
    * @param number The passage's number.
    * @param order The order of the passage.
    */
   public Passage (int id, String text, int number, int order)
   {
      super (id, text);
      this.m_Order = order;
      this.m_Number = number;
   }

   /**
    * Gets the passage's text.
    * @return Text of the passage.
    */
   public String getText ()
   {
      return this.getName ();
   }

   /**
    * Gets the order of the passage.
    * @return The passage's order.
    */
   public int getOrder ()
   {
      return this.m_Order;
   }

   /**
    * Gets the passage's number.
    * @return The passage's number.
    */
   public int getNumber ()
   {
      return this.m_Number;
   }

   /**
    * Gets the passage's header.
    * @return The passages header object, null if none.
    */
   public BaseDbObject getHeader ()
   {
      return this.m_Header;
   }

   /**
    * Sets the header for the passage.
    * @param id ID of the header.
    * @param text Text of the header.
    */
   public void setHeader (int id, String text)
   {
      this.m_Header = new BaseDbObject (id, text);
   }

   /**
    * Gets the list of footers for the passage.
    * @return Footer list.
    */
   public Map<Integer, BaseDbObject> getFooters ()
   {
      return this.m_Footers;
   }

   /**
    * Adds the given footer to the passage.
    * @param id ID of footer.
    * @param text Text of footer.
    * @param index Index of footer.
    */
   public void addFooter (int id, String text, int index)
   {
      if (this.m_Footers == null) {
         this.m_Footers = new HashMap<> ();
      }
      this.m_Footers.put (index, new BaseDbObject (id, text));
   }

   /**
    * Gets the list of links for the passage.
    * @return The list of links for the passage.
    */
   public List<Link> getLinks ()
   {
      return this.m_Links;
   }

   /**
    * Adds the given link.
    * @param link Link to add.
    */
   public void addLink (Link link)
   {
      if (this.m_Links == null) {
         this.m_Links = new ArrayList<> ();
      }
      this.m_Links.add (link);
   }

   /**
    * Adds a link to the passage with the given information.
    * @param id ID of link.
    * @param startIndex Start index of link.
    * @param endIndex End index of link.
    * @param area Area of link.
    * @param link The actual link.
    */
   public void addLink (int id, int startIndex, int endIndex, String area, String link)
   {
      Link linkItem = new Link (id, link);
      linkItem.setStartIndex (startIndex);
      linkItem.setEndIndex (endIndex);
      linkItem.setArea (area);
      this.addLink (linkItem);
   }

   /**
    * Gets the list of definitions for the passage.
    * @return The list of definitions for the passage.
    */
   public List<Link> getDefinitions ()
   {
      return this.m_Definitions;
   }

   /**
    * Adds the given definition.
    * @param definition Definition to add.
    */
   public void addDefinition (Link definition)
   {
      if (this.m_Definitions == null) {
         this.m_Definitions = new ArrayList<> ();
      }
      this.m_Definitions.add (definition);
   }

   /**
    * Adds a definition to the passage with the given information.
    * @param id ID of definition.
    * @param startIndex Start index of definition.
    * @param endIndex End index of definition.
    * @param item The definition item.
    */
   public void addDefinition (int id, int startIndex, int endIndex, String item)
   {
      Link linkItem = new Link (id, item);
      linkItem.setStartIndex (startIndex);
      linkItem.setEndIndex (endIndex);
      linkItem.setArea ("glossary");
      this.addDefinition (linkItem);
   }

   /**
    * Gets the list of styles.
    * @return The list of styles for the passage, null of none exist.
    */
   public List<Style> getStyles ()
   {
      return this.m_Styles;
   }

   /**
    * Adds the given style to the passage.
    * @param style Style to add.
    */
   public void addStyle (Style style)
   {
      if (this.m_Styles == null) {
         this.m_Styles = new ArrayList<> ();
      }
      this.m_Styles.add (style);
   }

   /**
    * Adds a new style with the given information.
    * @param passageStyleId Passage Style ID.
    * @param start Start index of style.
    * @param end End index of style.
    * @param styleId Id of the style.
    * @param typeId The ID of the style type.
    * @param style The style if any.
    */
   public void addStyle (int passageStyleId, int start, int end, int styleId, int typeId, String style)
   {
      Style s = new Style (styleId, typeId, style);
      s.setEndIndex (end);
      s.setStartIndex (start);
      s.setPassageStyleId (passageStyleId);
      this.addStyle (s);
   }
}
